﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MockDataTransferObject;
using System.Data;
using MockCommon;
using System.Data.SqlClient;
using System.Collections.ObjectModel;

namespace MockDataAccess
{
    public class ContractDataAccess : BaseDataAccess<Contract>
    {
        private Collection<SqlParameter> _parameters;
        
        /// <summary>
        /// Initializes a new instance of the <see cref="ContractDataAccess"/> class.
        /// </summary>
        public ContractDataAccess()
            : base()
        {
        }
        
        /// <summary>
        /// Initializes a new instance of the <see cref="ContractDataAccess"/> class.
        /// </summary>
        /// <param name="isFake">if set to <c>true</c> [is fake].</param>
        public ContractDataAccess(bool isFake)
            : base(isFake)
        {
        }

        #region Is
        #endregion

        #region Get
        /// <summary>
        /// Gets the contract id by user id.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <returns></returns>
        public object GetContractIdByUserId(Guid userId)
        {
            _parameters = new Collection<SqlParameter>();
            _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterUserId, Value = userId });
            return ExecuteQueryAsScalar(Constant.ContractGetByContractorUserId, _parameters);
        }

        /// <summary>
        /// Retrieves all contracts.
        /// </summary>
        /// <returns></returns>
        public Collection<Contract> RetrieveAllContracts()
        {
            try
            {
                DataTable table = ExecuteQueryAsDataTable(Constant.ContractGetAll, null);
                Collection<AbstractEntity> entities = ConvertToList(table, typeof(Contract));
                return new Collection<Contract>(entities.Cast<Contract>().ToList());
            }
            catch (ArgumentException exception)
            {
                throw new MockException(exception.Message, "DataAccess");
            }
            catch (InvalidCastException exception)
            {
                throw new MockException(exception.Message, "DataAccess");
            }
        }

        /// <summary>
        /// Gets the by consultant id.
        /// </summary>
        /// <param name="consultantId">The consultant id.</param>
        /// <returns></returns>
        public Collection<Contract> GetByConsultantId(object consultantId)
        {
            try
            {
                _parameters = new Collection<SqlParameter>();
                _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterConsultantId, Value = consultantId });
                DataTable table = ExecuteQueryAsDataTable(Constant.ContractGetByConsultantId, _parameters);
                Collection<AbstractEntity> entities = ConvertToList(table, typeof(Contract));
                return new Collection<Contract>(entities.Cast<Contract>().ToList());
            }
            catch (ArgumentException exception)
            {
                throw new MockException(exception.Message, "DataAccess");
            }
            catch (InvalidCastException exception)
            {
                throw new MockException(exception.Message, "DataAccess");
            }
        }

        /// <summary>
        /// Gets the by filter.
        /// </summary>
        /// <param name="contractTypeId">The contract type id.</param>
        /// <param name="isActive">The is active.</param>
        /// <param name="consultantId">The consultant id.</param>
        /// <returns></returns>
        public Collection<Contract> GetByFilter(object contractTypeId, object isActive, object consultantId)
        {
            try
            {
                _parameters = new Collection<SqlParameter>();
                _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterContractTypeId, Value = contractTypeId.ToString() });
                if (isActive != null && !String.IsNullOrEmpty(isActive.ToString()))
                {
                    bool isActiveBool = (bool)isActive;
                    _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterIsActive, Value = isActiveBool ? "1" : "0" });
                }
                else
                {
                    _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterIsActive, Value = "" });
                }
                _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterConsultantId, Value = consultantId.ToString() });
                DataTable dataTable = ExecuteQueryAsDataTable(Constant.ContractGetByFilter, _parameters);
                Collection<AbstractEntity> entities = ConvertToList(dataTable, typeof(Contract));
                return new Collection<Contract>(entities.Cast<Contract>().ToList());
            }
            catch (ArgumentException exception)
            {
                throw new MockException(exception.Message, "DataAccess");
            }
            catch (InvalidCastException exception)
            {
                throw new MockException(exception.Message, "DataAccess");
            }
        }
        #endregion
    }

}
